# -*- coding: utf-8 -*-
"""
Created on Mon Mar  7 14:38:46 2011
Plot pca test error vs train error
@author: -
"""

# Computes the gaussian gradients on a boxm_alpha_scene

import os;
import optparse;
import time;
import sys;
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cmt
import glob


#Parse inputs
print ("******************************Averaging Accuracy***************************")


trials_dir = "/Users/isa/Experiments/BOF/helicopter_providence/taylor/bof_cross_validation";
trials = [0,1,2,3,4,5,6,7,8,9];
ncategories = 5;

  
if not os.path.isdir(trials_dir +"/"):
  print "Invalid trials Dir"
  sys.exit(-1);

#confusion matrix percent
cm_avg = np.zeros((ncategories,ncategories));
cm_var = np.zeros((ncategories,ncategories));

for t in trials:
    
    cm_file=trials_dir +"/trial_" + str(t) + "/classification_20/confussion_matrix.txt";
    
    #confusion matrix percent
    cm = np.genfromtxt(cm_file);
         
    cm_avg = cm_avg + cm;
    cm_var = cm_var +  cm*cm;    
         
cm_avg = cm_avg/len(trials);
cm_var = cm_var/len(trials) - cm_avg*cm_avg;
           
print cm_avg
print cm_var

plt.imsave(trials_dir + "/avg_confussion_matrix.png", cm_avg ,  cmap=cmt.gray)
plt.imsave(trials_dir + "/var_confussion_matrix.png", cm_var ,  cmap=cmt.gray)


cm_file = trials_dir + "/avg_confussion_matrix.txt"
np.savetxt(cm_file, cm_avg);

cm_file = trials_dir + "/var_confussion_matrix.txt"
np.savetxt(cm_file, cm_var); 
cm_file = trials_dir + "/std_confussion_matrix.txt"
np.savetxt(cm_file, np.sqrt(cm_var));